package com.imonsoft.sqm.web.basedata;

import com.imonsoft.common.constant.CCommon;
import com.imonsoft.common.constant.ConstantDict;
import com.imonsoft.common.constant.ConstantSqm;
import com.imonsoft.common.exception.SystemException;
import com.imonsoft.common.httpModel.DataGrid;
import com.imonsoft.common.httpModel.Pager;
import com.imonsoft.common.httpModel.ReturnMessage;
import com.imonsoft.common.util.AjaxUtil;
import com.imonsoft.common.util.JsonUtil;
import com.imonsoft.sqm.pojo.basedata.Dictionary;
import com.imonsoft.sqm.pojo.basedata.Part;
import com.imonsoft.sqm.service.basedata.DictionaryService;
import com.imonsoft.sqm.service.basedata.PartService;
import com.imonsoft.sqm.validator.basedata.PartValidator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/basedata/part")
public class PartController {
    protected final Log logger = LogFactory.getLog(getClass());
    @Autowired
    private PartService partService;
    @Autowired
    private PartValidator partValidator;
    @Autowired
    private DictionaryService dictionaryService;


    @RequestMapping("/listUI.do")
    public void listUI(ModelMap model) {
        List<Dictionary> dictBaseDataStatusList = dictionaryService.findDictionaryByType(ConstantDict.BASE_DATA_STATUS);
        model.put("dictBaseDataStatusList", dictBaseDataStatusList);
        List<Dictionary> dictPartTypeList=dictionaryService.findDictionaryByType(ConstantDict.PART_PART_TYPE);
        model.put("dictPartTypeList", dictPartTypeList);
        List<Dictionary> dictProductTypeList=dictionaryService.findDictionaryByType(ConstantDict.PART_PRODUCT_TYPE);
        model.put("dictProductTypeList", dictProductTypeList);
    }

    @RequestMapping(value = "/ajaxList.do", method = RequestMethod.POST)
    public void ajaxList(HttpServletResponse response, Pager pager, Part part) {
        DataGrid<Part> dataGrid = new DataGrid<Part>();
        try {
            dataGrid = partService.getDataGrid(part, pager);
        } catch (SystemException se) {
            logger.info(se.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        AjaxUtil.renderJSON(response, JsonUtil.bean2json(dataGrid));
    }

    @RequestMapping("/addUI.do")
    public void addUI(ModelMap model) {
        List<Dictionary> dictPartTypeList=dictionaryService.findDictionaryByType(ConstantDict.PART_PART_TYPE);
        model.put("dictPartTypeList", dictPartTypeList);
        List<Dictionary> dictProductTypeList=dictionaryService.findDictionaryByType(ConstantDict.PART_PRODUCT_TYPE);
        model.put("dictProductTypeList", dictProductTypeList);
    }

    @RequestMapping(value = "/ajaxAdd.do", method = RequestMethod.POST)
    public void ajaxAdd(HttpServletResponse response, Part part, BindingResult result) {
        ReturnMessage message = new ReturnMessage();
        try {
            partValidator.methodAddValidate(part, result);
            if (result.hasErrors()) {
                message.setObj(partValidator.getErrors(result));
            } else {
                partService.insert(part);
                message.setSuccess(true);
                message.setMsg(ConstantSqm.OPERATE_SUCCESS);
            }
        } catch (SystemException se) {
            logger.info(se.getMessage());
            message.setMsg(se.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage());
            message.setMsg(CCommon.EXCEPTION_MSG);
        }
        AjaxUtil.renderJSON(response, JsonUtil.bean2json(message));
    }

    @RequestMapping(value = "/modifyUI.do")
    public void modifyUI(ModelMap model,Long id) {
        if(null!=id){
            Part part = partService.get(id);
            model.put("part", part);
        } else {
            throw new SystemException(ConstantSqm.DATA_ERROR);
        }
        List<Dictionary> dictPartTypeList=dictionaryService.findDictionaryByType(ConstantDict.PART_PART_TYPE);
        model.put("dictPartTypeList", dictPartTypeList);
        List<Dictionary> dictProductTypeList=dictionaryService.findDictionaryByType(ConstantDict.PART_PRODUCT_TYPE);
        model.put("dictProductTypeList", dictProductTypeList);
    }

    @RequestMapping(value = "/ajaxModify.do", method = RequestMethod.POST)
    public void ajaxModify(HttpServletResponse response, Part part, BindingResult result) {
        ReturnMessage message = new ReturnMessage();
        try {
            partValidator.methodModifyValidate(part, result);
            if (result.hasErrors()) {
                message.setObj(partValidator.getErrors(result));
            } else {
                partService.update(part);
                message.setSuccess(true);
                message.setMsg(ConstantSqm.OPERATE_SUCCESS);
            }

        } catch (SystemException se) {
            logger.info(se.getMessage());
            message.setMsg(se.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage());
            message.setMsg(CCommon.EXCEPTION_MSG);
        }
        AjaxUtil.renderJSON(response, JsonUtil.bean2json(message));
    }

    @RequestMapping(value = "/ajaxStop.do", method = RequestMethod.POST)
    public void ajaxStop(HttpServletResponse response, @RequestParam("ids") List<Long> idList) {
        ReturnMessage message = new ReturnMessage();
        try {
            partService.stopPart(idList);
            message.setSuccess(true);
            message.setMsg(ConstantSqm.OPERATE_SUCCESS);
        } catch (SystemException se) {
            logger.info(se.getMessage());
            message.setMsg(se.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage());
            message.setMsg(CCommon.EXCEPTION_MSG);
        }
        AjaxUtil.renderJSON(response, JsonUtil.bean2json(message));
    }
    @RequestMapping(value = "/ajaxStart.do", method = RequestMethod.POST)
    public void ajaxStart(HttpServletResponse response, @RequestParam("ids") List<Long> idList) {
        ReturnMessage message = new ReturnMessage();
        try {
            partService.startPart(idList);
            message.setSuccess(true);
            message.setMsg(ConstantSqm.OPERATE_SUCCESS);
        } catch (SystemException se) {
            logger.info(se.getMessage());
            message.setMsg(se.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage());
            message.setMsg(CCommon.EXCEPTION_MSG);
        }
        AjaxUtil.renderJSON(response, JsonUtil.bean2json(message));
    }
    @RequestMapping("/dialogUI.do")
    public void dialogUI(ModelMap model,Long supplierId) {
        if(null!=supplierId){
            model.put("supplierId",supplierId);
        } else {
            throw new SystemException(ConstantSqm.DATA_ERROR);
        }
    }
}
